package cn.itsource.pethome.pet.service.impl;

import cn.itsource.pethome.basic.service.impl.BaseServiceImpl;
import cn.itsource.pethome.org.domain.Shop;
import cn.itsource.pethome.org.mapper.ShopMapper;
import cn.itsource.pethome.pet.domain.SearchMasterMessage;
import cn.itsource.pethome.pet.mapper.SearchMasterMessageMapper;
import cn.itsource.pethome.pet.service.ISearchMasterMessageService;
import cn.itsource.pethome.utils.AjaxResult;
import cn.itsource.pethome.utils.DistanceUtil;
import cn.itsource.pethome.utils.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SearchMasterMessageServiceImpl extends BaseServiceImpl<SearchMasterMessage> implements ISearchMasterMessageService {

    @Autowired
    private SearchMasterMessageMapper searchMasterMessageMapper;
    @Autowired
    private ShopMapper shopMapper;

    @Override
    public AjaxResult publish(SearchMasterMessage searchMasterMessage) {
        try {
            //将最近的门店信息存到对象中
            //先加载出所有门店
            List<Shop> shopList = shopMapper.loadAll();
            //得到用户寻主消息中地址的经纬度
            Point point = DistanceUtil.getPoint(searchMasterMessage.getAddress());
            //找到最近的店铺
            Shop nearestShop = DistanceUtil.getNearestShop(point, shopList);
            searchMasterMessage.setShop(nearestShop);
            //保存寻主信息
            searchMasterMessageMapper.save(searchMasterMessage);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("网络延迟，请稍后再试");
        }
    }

    @Override
    public AjaxResult batchProcessing(Long[] ids) {
        try {
            searchMasterMessageMapper.batchProcessing(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("网络异常，请稍后再试");
        }
    }
}
